﻿using chihuopig.IService;
using System;
using System.Collections.Generic;
using System.Text;
using chihuopig.Model;
using System.Threading.Tasks;

namespace chihuopig.Service
{
    public class RoleService : DBService, IRoleService
    {
        public Task<int> delAll(long[] Id)
        {
            return Task.Factory.StartNew<int>(s => {
                return Sqlclient.Ado
               .ExecuteCommand($"update Role set IsDelete='1' where id in ({string.Join(",", (long[])s)})");
            }, Id);
        }

        public Task<int> Deleteable(long Id)
        {
            return Sqlclient
                        .Updateable<Role>()
                        .UpdateColumns(it => new Role() { IsDelete = true })
                        .Where(c => c.Id == Id)
                        .ExecuteCommandAsync();
        }

        public Task<Role> EditAsync(long Id)
        {
            return Sqlclient.Queryable<Role>().Where(s => s.Id == Id&&s.IsDelete==false).FirstAsync();
        }

        public Task<List<long>> GetAdminRoleId(long AdminId)
        {
           return Sqlclient.Queryable<AdminRole>().Where(c => c.AdminId == AdminId).Select(s => s.RoleId).ToListAsync();
        }

        public Task<List<Role>> GetPage(int pageIndex, int PageSize)
        {
            return Sqlclient.Queryable<Role>().Where(c => c.IsDelete == false).OrderBy(s => s.Addtime, SqlSugar.OrderByType.Desc).ToPageListAsync(pageIndex, PageSize);
        }

        public Task<double> GetPageTotal(int PageSize)
        {
            return Task.Factory.StartNew<double>(a =>
            {
                double size = Convert.ToDouble(a);
                double bnum = Sqlclient.Queryable<Role>().Where(c => c.IsDelete == false).OrderBy(s => s.Addtime, SqlSugar.OrderByType.Desc).Count() / size;
                //Math.Ceiling(bnum);
                return Math.Ceiling(bnum);

            }, PageSize);
        }

        public Task<List<Role>> GetTable()
        {
            return Sqlclient.Queryable<Role>().Where(s=>s.IsDelete==false).ToListAsync();
        }

        public Task<long> Insertable(Role Model)
        {
            return Sqlclient.Insertable(Model).ExecuteReturnBigIdentityAsync();
        }

        public Task<int> Updateable(Role Model)
        {
            return Sqlclient.Updateable(Model).ExecuteCommandAsync();
        }
    }
}
